﻿@using RiverCruise.Models
@model RiverCruise.Models.BasePagingModel

<div class="container-fluid text-center">
    <nav>
        <ul class="pagination" data-rc-pagertarget="@Model.TargetDiv">
            @if (Model.CurrentPage != 1)
            {
                <li class="pageBlock">
                    <a href="@Url.Action(@Model.Action, @Model.Controller, new { page = Model.CurrentPage - 1, searchText = Model.SearchText }, null)"><span aria-hidden="true">«</span><span class="sr-only">Previous</span></a>
                </li>
            }

            @{
                var pagesToShow = new List<int>();
                int currentPage = Model.CurrentPage;
                int i = 0;
                const int totalPageNumbers = BasePagingModel.TotalPageNumbers;
                while (currentPage - i >= 1 && i <= totalPageNumbers / 2)
                {
                    pagesToShow.Add(currentPage - i);
                    i++;
                }

                int j = 1;
                while (i <= totalPageNumbers && currentPage + j <= Model.Pages)
                {
                    pagesToShow.Add(currentPage + j);
                    i++;
                    j++;
                }

                if (i <= totalPageNumbers)
                {
                    var k = pagesToShow.Min(x => x) - 1;
                    while (k >= 1 && i <= totalPageNumbers)
                    {
                        pagesToShow.Add(k);
                        k--;
                        i++;
                    }
                }
            }

            @foreach (var page in pagesToShow.OrderBy(q => q))
            {
                var hiddenOnSmall = page > currentPage +2 || page < currentPage -2 ? "hidden-xs" : string.Empty;
                
                <li class="@(page == Model.CurrentPage ? "active pageBlock" : "pageBlock "+hiddenOnSmall)">
                    <a href="@Url.Action(Model.Action, Model.Controller, new { page = page, searchText = Model.SearchText })">
                        @page<span class="sr-only">(current)</span>
                    </a>
                </li>
            }

            @if (Model.CurrentPage < Model.Pages)
            {
                <li class="pageBlock">
                    <a href="@Url.Action(Model.Action, @Model.Controller, new { page = Model.CurrentPage + 1, searchText = Model.SearchText }, null)"><span aria-hidden="true">»</span><span class="sr-only">Next</span></a>
                </li>
            }
        </ul>

    </nav>
</div>
